local hongye = fk.CreateSkill {
  name = "th_hongye",
  tags = { Skill.Compulsory },
}

hongye:addEffect(fk.BeforeCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hongye.name) and table.find(player.room:getOtherPlayers(player, false), function(p) return #p:getCardIds("h") >= #player:getCardIds("h") end) then
      for _, move in ipairs(data) do
        if move.from and move.moveReason == fk.ReasonDiscard and move.skillName == "phase_discard" and move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local card = Fk:getCardById(info.cardId)
            if card.color == Card.Red then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:cancelMove(data, nil, function(move, info)
      if move.from and move.moveReason == fk.ReasonDiscard and move.skillName == "phase_discard" and move.toArea == Card.DiscardPile then
        local c = Fk:getCardById(info.cardId)
        return c.color == Card.Red
      end
      return false
    end)
    -- local cards = {}
    -- for _, move in ipairs(data) do
    --   if move.from and move.moveReason == fk.ReasonDiscard and move.skillName == "phase_discard" and move.toArea == Card.DiscardPile then
    --     local infos = {}
    --     for _, info in ipairs(move.moveInfo) do
    --       local c = Fk:getCardById(info.cardId)
    --       if c.color == Card.Red then
    --         table.insertIfNeed(cards, c)
    --       else
    --         table.insert(infos, info)
    --       end
    --     end
    --     move.moveInfo = infos
    --   end
    -- end
    local others = table.filter(cards, function(cid) return room:getCardOwner(cid) ~= player end)
    if #others > 0 then
      room:obtainCard(player, others, true, fk.ReasonJustMove, player, hongye.name)
    end
  end
})
hongye:addEffect("filter", {
  card_filter = function(self, to_select, player, isJudgeEvent)
    return player:hasSkill(hongye.name) and to_select.suit == Card.Club and
    (table.contains(player:getCardIds("he"), to_select.id) or isJudgeEvent)
  end,
  view_as = function(self, player, to_select)
    return Fk:cloneCard(to_select.name, Card.Diamond, to_select.number)
  end,
})

Fk:loadTranslationTable{
  ["th_hongye"] = "红叶",
  [":th_hongye"] = "锁定技，你的♣牌视为<font color='red'>♦</font>牌；一名角色弃牌阶段弃置牌时，若你的手牌数不为唯一最多，你获得其中所有红色牌。",
}

return hongye